package org.itworks.biodev.gui.panels;

import java.awt.event.ActionEvent;

import javax.swing.JOptionPane;

import org.itworks.biodev.gui.*;
import org.itworks.biodev.model.*;
import org.itworks.helpers.mvc.*;

public class ProductsPanelControler extends AbstractControler {

	private ProductsPanelModel model;

	public ProductsPanelControler(ProductsPanelModel model) {
		this.model = model;
	}
	
	public ProductsPanelModel getModel() {
		return this.model;
	}

	@Override
	public void viewLoaded() {
		try {
			this.model.setProductCategoriesList(ProductCategoryTable.getInstance().findAll());
			this.model.setSubProductCategoriesList(ProductSubCategoryTable.getInstance().findAll());
			this.model.setProductsList(ProductTable.getInstance().findAll());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void addNewSubCategoryButtonActionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub
		EditSubCategoryDialogView dialog = new EditSubCategoryDialogView(new EditSubCategoryDialogControler(new EditSubCategoryDialogModel(), null));
		dialog.setModal(true);
		dialog.setVisible(true);
		if(dialog.getIsOK()) {
			try {
				model.setSubProductCategoriesList(ProductSubCategoryTable.getInstance().findAll());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void addNewCategoryButtonActionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub
		String categoryName = JOptionPane.showInputDialog(null, "Entrez le nom de la nouvelle categorie :", "", JOptionPane.QUESTION_MESSAGE);
		if(categoryName != null) {
			ProductCategory newCategory = new ProductCategory();
			newCategory.setLabel(categoryName);
			try {
				if(ProductCategoryTable.getInstance().existsByName(categoryName.toLowerCase())) {
					JOptionPane.showMessageDialog(null, "La categorie de produits existe deja !", "Erreur", JOptionPane.ERROR_MESSAGE);
				} else {
					if(ProductCategoryTable.getInstance().add(newCategory) == false) {
						JOptionPane.showMessageDialog(null, "Impossible d'ajouter la catégorie de produits... Contactez le support si cette erreur se reproduit.", "Erreur", JOptionPane.ERROR_MESSAGE);
					} else {
						this.model.addProductCategory(newCategory);
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void buttonColumnProductCategoriesActionPerformed(
			ActionEvent actionEvent, int selectedIndex) {
		// TODO Auto-generated method stub
		if(selectedIndex >= 0) {
			ProductCategory selectedProductCategory = model.getProductCategoriesList().elementAt(selectedIndex);
			String newCategoryName = JOptionPane.showInputDialog("Entrez le nouveau nom de la categorie :", selectedProductCategory.getLabel());
			if(newCategoryName != null) {
				selectedProductCategory.setLabel(newCategoryName);
				ProductCategoryTable.getInstance().update(selectedProductCategory);
				try {
					this.model.setProductCategoriesList(ProductCategoryTable.getInstance().findAll());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public void buttonColumnProductSubCategoriesActionPerformed(ActionEvent actionEvent, int selectedIndex) {
		if(selectedIndex >= 0) {
			ProductSubCategory selectedProductSubCategory = model.getProductSubCategoriesList().elementAt(selectedIndex);
			EditSubCategoryDialogView dialog =
					new EditSubCategoryDialogView(
						new EditSubCategoryDialogControler(
							new EditSubCategoryDialogModel(),
							selectedProductSubCategory
						)
					);
				dialog.setModal(true);
				dialog.setVisible(true);
				if(dialog.getIsOK()) {
					try {
						model.setSubProductCategoriesList(ProductSubCategoryTable.getInstance().findAll());
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
		}
	}

	public void addNewProductButtonActionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub
		EditProductDialogView dialog =
			new EditProductDialogView(
				new EditProductDialogControler(
					new EditProductDialogModel(),
					null
				)
			);
		dialog.setModal(true);
		dialog.setVisible(true);
		if(dialog.isOk()) {
			try {
				this.model.setProductsList(ProductTable.getInstance().findAll());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
